#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from datetime import datetime


import pymysql

from flask_sqlalchemy import SQLAlchemy

from .import app

from .utils import calc_md5




# 指定使用的数据库的链接地址
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:root@localhost:3306/myblog?charset=utf8"
# 关闭追踪数据库的修改
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

# 创建一个SQLAlchemy对象,需要放在config后面
db = SQLAlchemy(app)


class Article(db.Model):
	__tablename__ = "article"
	id = db.Column(db.Integer, primary_key=True, autoincrement =True)
	title = db.Column(db.String(300), unique=True, nullable=False)
	author = db.Column(db.String(300),nullable=False)
	content = db.Column(db.Text, nullable=False)
	publish_addr = db.Column(db.String(300), nullable=False)
	publish_time = db.Column(db.DateTime, default=datetime.now())
	img_path = db.Column(db.String(300))
	tag = db.Column(db.String(300), unique=False)
	is_valid = db.Column(db.Boolean, default=1)
	crawl_time = db.Column(db.DateTime,nullable=False, default=datetime.now())
	point_up_num = db.Column(db.Integer, default=0)
	bookmark_num =db.Column(db.Integer, default=0)



	def __init__(self, id, title, author, tag, content, point_up_num,bookmark_num,publish_addr, publish_time, img_path, is_valid, crawl_time):
		self.id = id
		self.tag = tag
		self.title = title
		self.author = author
		self.content = content
		self.publish_addr = publish_addr
		self.publish_time = publish_time
		self.img_path = img_path
		self.is_valid = is_valid
		self.crawl_time = crawl_time
		self.point_up_num = point_up_num
		self.bookmark_num = bookmark_num

	def __repr__(self):
		return '%r' % self.title


class User(db.Model):
	"""docstring for User"""

	__tablename__ = "user"

	id  = db.Column(db.Integer,primary_key=True,autoincrement =True)
	is_master = db.Column(db.Boolean, default=False)
	age = db.Column(db.Integer,nullable=False, default=18)
	name = db.Column(db.String(50),nullable=False)
	gender = db.Column(db.Integer,nullable=False,default=0)
	email = db.Column(db.String(50), unique=True)
	e_password = db.Column(db.String(100),nullable=False)
	is_valid = db.Column(db.Boolean, default=1)
	register_time = db.Column(db.DateTime, default=datetime.now())


	def __init__(self,e_password,name,id=None, email=None, gender=1, age=18,is_valid=1,register_time=datetime.now()):
		self.id = id
		self.age = age
		self.name = name
		self.email = email
		self.gender = gender
		self.is_valid = is_valid
		self.e_password = e_password
		self.register_time = register_time 


	def __repr__(self):
		return '<User {0!r}>'.format(self.name)
        
	def to_dict(self):
		return {
			'id' : self.id,
			'age' : self.age,
			'name' : self.name,
			'email' : self.email,
			'gender' : self.gender,
			'is_valid' : self.is_valid,
			'e_password' : self.e_password,
			'register_time' : self.register_time
		}
	@staticmethod
	def init_user():
		'''初始化用户（确保至少有一个[原始]用户）'''
		cursor = User.query
		if cursor.count() < 1:
			usr = User(name='admin', e_password=calc_md5('123456'))
			db.session.add(usr)
			db.session.commit()
			print(usr.to_dict())
		else:
			print('用户已存在:')
			print(cursor.first().to_dict())

class Comment(db.Model):
	__tablename__ = "comment"

	id = db.Column(db.Integer,primary_key=True,autoincrement =True)
	comment = db.Column(db.Text)
	name_id = db.Column(db.Integer, db.ForeignKey('user.id'))
	article_id = db.Column(db.Integer, db.ForeignKey('article.id'))

	def __init__(self, id, comment, name_id, article_id):

		self.id = id
		self.name_id = name_id
		self.comment = comment
		self.article_id = article_id

	def __repr__(self):
		return '<Comment {0!r}>'.format(self.comment)
		
# if __name__ == '__main__':
	# 初始化表格
db.create_all()
	# 初始化管理员
User.init_user()
